/**
 * 公共提示及常用工具 author:gsh create at 2016
 */
var msgWnd = window.parent.layer;
$(function() {
	msgWnd = window.parent.layer;
	msgWnd.config({
		extend : 'extend/layer.ext.js'
	});
});
var msgTools = {
	alert : function(_title, _content, _timeout, _callback) {
		if (typeof (_timeout) == "undefined")
			_timeout = 2000;
		msgWnd.msg(_content, {
			title : _title,
			skin : 'layui-layer-lan',
			time : _timeout,
			icon : 1,
			end:function(){
				if (typeof (_callback) != "undefined" && typeof(_callback)=="function" ) {
					_callback(_content);
				}				
			}
		});

	},
	closeFra : function(_timeout, _isRefresh, _callback) {
		if (typeof (_timeout) == "undefined")
			_timeout = 100;
		setTimeout(function() {
			if (_isRefresh) {
				var mainFra = parent.window.$("#rightFrame").get(0);
				if (typeof (mainFra.contentWindow.reloadPg) == "undefined")
					mainFra.src = mainFra.contentWindow.location.href;
				else
					mainFra.contentWindow.reloadPg();
			}try{
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
			}catch(ex){}
		}, _timeout);
		if (typeof (_callback) != "undefined" && typeof(_callback)=="function" ) {
			_callback(_content);
		}
	},
	alertCloseFra : function(_title, _content, _timeout, _callback) {
		this.closeFra(100);
		this.alert(_title, _content, _timeout, _callback);
	},
	alertCloseFraRefresh : function(_title, _content, _timeout, _isRefresh,_callback) {
		this.closeFra(100, _isRefresh);
		this.alert(_title, _content, _timeout, _callback);
	},
	refresh:function(){
		if(typeof(reloadPg)!="undefined")
			reloadPg();
		else
			window.location.href = window.location.href;
	}
};

$.fn.extend({
	autoHeight : function() {
		var ifm = $(this);
		ifm.load(function() {
			var wnd = ifm.get(0).contentWindow;
			if (wnd != null) {
				ifm.height(wnd.$.find("body")[0].scrollHeight);
			}
		});
	},autoFit:function(){
		var jthis=$(this);
		$.each(jthis,function(){
			var func=function(ifra){
				ifra.height($(window).height()-90);
			};
			func(jthis);
			$(window).resize(func); 
		});
	}
});

/** <div class='tags'>xxx</div>* */
$.fn.extend({
	tagsGrop : function(param) {
		var jthis = $(this);
		$.each(jthis, function(i, o) {
			$(o).click(function() {
				var tag = $(o);
				if (tag.hasClass("set"))
					tag.removeClass("set");
				else
					tag.addClass("set");
			});
			if (typeof (param) != "undefined"
					&& typeof (param.click) != "undefined") {
				$(o).click(param.click);
			}
		});
		$.extend(jthis, {
			selected : function() {
				var mp=jthis.map(function() {
					if ($(this).hasClass("set"))
						return this;
				});
				return mp.get();
			},
			selectedAttr : function(attrb) {
				var mp=$(jthis.selected()).map(function(){
					return $(this).attr(attrb);
				});
				return mp.get();
			}
		});
		return jthis;
	}
});

$.fn.extend({
	imgEdit : function(param) {
		var jthis = $(this);
		$.each(jthis, function(i, o) {
			var jitem = $(o);
			var jBtnEdit=jitem.find(".btn-edit");
			var func= function() {
				jBtnEdit.show();
				return false;
			};
			jitem.mousemove(func);
			jBtnEdit.mousemove(func);
			$("body").mouseout(function(){
				jBtnEdit.hide();
				return false;
			});
			
			if (typeof (param) != "undefined" && typeof (param.click) != "undefined") {
				jBtnEdit.click(param.click);
			}
		});
		return jthis;
	}
});
